/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.dao;

import org.zhiqim.account.ZacConstants;
import org.zhiqim.account.dbo.user.UserAccount;
import org.zhiqim.account.dbo.user.UserInfo;
import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.orm.ZTable;
import org.zhiqim.orm.dbo.Selector;

@AnAlias("UserPassportDao")
public class UserPassportDao implements ZacConstants
{
    /** 找回密码，查询用户账号 */
    public static String getOtherUserAccount(String userAccount) throws Exception
    {
        UserAccount account = Global.get(ZTable.class).item(UserAccount.class, new Selector("userAccount", userAccount));
        if (account == null)
            return null;
        
        //先查登录对应的账号，手机的查邮箱，邮箱的查手机
        Selector selector = new Selector();
        selector.addMust("userId", account.getUserId());
        selector.addMust("accountType", account.getAccountType() == ACCOUNT_TYPE_MOBILE?ACCOUNT_TYPE_EMAIL:ACCOUNT_TYPE_MOBILE);
        
        UserAccount other = Global.get(ZTable.class).item(UserAccount.class, selector);
        if (other != null)
            return other.getUserAccount();

        //再查绑定的账号
        UserInfo user = Global.get(ZTable.class).item(UserInfo.class, account.getUserId());
        if (user == null)
            return null;
        
        return (account.getAccountType() == ACCOUNT_TYPE_MOBILE)?user.getUserEmail():user.getUserMobile();
    }
    
    /** 验证是否已绑定QQ */
    public static boolean bindByQQ(long userId) throws Exception
    {
        Selector selector = new Selector();
        selector.addMust("userId", userId);
        selector.addMust("accountType", ACCOUNT_TYPE_QQ);
        UserAccount account = Global.get(ZTable.class).item(UserAccount.class, selector);
        if (account != null)
            return true;
        
        return false;
    }
    
    /** 验证是否已绑定微信 */
    public static boolean bindByWx(long userId) throws Exception
    {
        Selector selector = new Selector();
        selector.addMust("userId", userId);
        selector.addMust("accountType", ACCOUNT_TYPE_WXOP);
        UserAccount account = Global.get(ZTable.class).item(UserAccount.class, selector);
        if (account != null)
            return true;
        
        return false;
    }
    
    /** 验证是否已实名认证 */
    public static boolean certify(long userId) throws Exception
    {
        UserInfo user = Global.get(ZTable.class).item(UserInfo.class, new Selector("userBalance,certStatus,certName,certNo", "userId", userId));
        if (user.getCertStatus() == 1)
            return true;
        
        return false;
    }
}
